{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 显示如何使用 SLURM ( 天河 )\n",
    "\n",
    "## 其实这是个 jupyter notebook 的演示"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## yhinfo  (别名 yhi)\n",
    "\n",
    "yhi 可用来查看 分区表的信息 ， 结果输出信息中写明了 作业分区名 ， 状态 ， 时间限制 ， 节点数 ， 状态 ， 节点列表等信息  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PARTITION  AVAIL  TIMELIMIT  NODES  STATE NODELIST\r\n",
      "nsfc3         up   infinite    208   idle cn[6924,6926,6943,6945,6955,6968,6986,6998,7000,7026,7038,7052,7095,7097,7104,7106,7110,7153,7157,7159,7180,7183,7193,7199,7204,7246,7267,7276,7285,7289,7296,7300,7311,7315,7317,7322,7326,7333,7338,7341,7343-7344,7354,7364,7380,7400,7414,7423,7684,7691,7693,7734,7745,7752,7756,7771,7811,7820,7837,7912,7914-7916,7924,7947,7953,7962-7963,7965,7968,7973,7979,7984,8032-8034,8037,8325,8333,8344,8359,8364-8365,8383,8419-8421,8459-8460,8473,8482,8485,8495,8498,8504,8506,8523,8528-8529,8541,8543,8550,8554,8556,8559,8561,8565,8568-8569,8573,8590,8593,8603-8604,8611,8632,8638-8639,8651,8653-8654,8658-8662,8675,8678,8693,8720,8728,8731,8733,8741-8742,8750,8759-8760,8772,8776,8780,8785,8804,8808,8812-8814,8820,8823-8824,8838-8840,8842,8849,8862,8900,8920,8937-8939,8943,8946-8947,8958,8960,8989,9011,9014-9015,9017-9018,9022-9023,9025,9029,9031-9032,9057-9059,9063,9075,9080,9083,9088,9096,9098,9113,9115,9119-9120,9124,9127-9128,9132-9133,9138,9141,9143,9146,9154,9177-9178,9195,9199-9200,9214]\r\n",
      
      "evaluating    up    4:00:00     68   idle cn[6917,6930,7001,7028,7074,7089,7120,7167,7178,7200,7215,7224,7231,7298,7337,7340,7342,7352,7381,7404-7405,7410,7689,7702,7729,7754-7755,7781,7785,7834,7880,7901,7925,7940,7954,7978,8337-8338,8340,8346,8438,8454,8483,8534,8539,8545,8588,8591,8597,8608,8637,8686,8747,8834,8898,8932,8956,9020,9038,9071,9076,9086-9087,9114,9131,9153,9158,9198]\r\n"
     ]
    }
   ],
   "source": [
    "yhinfo "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## yhqueue (别名 yhq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             JOBID PARTITION     NAME         USER ST       TIME  NODES NODELIST(REASON)\r\n"
     ]
    }
   ],
   "source": [
    "yhqueue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "yhq 用来显示作业队列的信息；目前我没有在等待和在运算的作业，所以队列里什么都没有。\n",
    "为了演示，我先用yhrun 来提交一个作业再查看。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 30547\r\n",
      "yhrun: error: Unable to allocate resources: User's group not permitted to use this partition\r\n",
      "             JOBID PARTITION     NAME         USER ST       TIME  NODES NODELIST(REASON)\r\n",
      "[1]+  Exit 1                  yhrun -n 1 sleep 60\r\n"
     ]
    }
   ],
   "source": [
    "yhrun -n 1 sleep 60 &\n",
    "yhqueue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里的出错信息显示我没有分区的权限，遇到这种情况，需要用户自己指定好作业分区。可用的分区可以通过上面的yhi 查看。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] 31309\r\n",
      "yhrun: job 377216 queued and waiting for resources\r\n",
      "bash: wait: pid 5 is not a child of this shell\r\n",
      "             JOBID PARTITION     NAME         USER ST       TIME  NODES NODELIST(REASON)\r\n",
      "            377216     nsfc3    sleep nscc-gz_jian PD       0:00      1 (Priority)\r\n"
     ]
    }
   ],
   "source": [
    "yhrun -n 1 -p nsfc3 sleep 60 &\n",
    "wait 5 \n",
    "yhqueue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里可以看到我的作业提交成功了，但是还没有分配到资源，所以在排队（PD , 实际是 pending ）."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             JOBID PARTITION     NAME         USER ST       TIME  NODES NODELIST(REASON)\r\n",
      "            377216     nsfc3    sleep nscc-gz_jian  R       0:14      1 cn7933\r\n"
     ]
    }
   ],
   "source": [
    "yhqueue\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里可以看到作业处于运行（Runing ）状态了 。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## yhrun "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yhrun: job 377225 queued and waiting for resources\r\n",
      "yhrun: job 377225 has been allocated resources\r\n",
      "cn8364\r\n",
      "[1]+  Done                    yhrun -n 1 -p nsfc3 sleep 60\r\n"
     ]
    }
   ],
   "source": [
    "yhrun -n 1 -p nsfc3 hostname "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "yhrun 就是实际最常用的提交命令了 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Usage: srun [OPTIONS...] executable [args...]\r\n",
      "\r\n",
      "Parallel run options:\r\n",
      "  -A, --account=name          charge job to specified account\r\n",
      "      --begin=time            defer job until HH:MM MM/DD/YY\r\n",
      "      --acctg-freq=<datatype>=<interval> accounting and profiling sampling\r\n",
      "                              intervals. Supported datatypes:\r\n",
      "                              task=<interval> energy=<interval>\r\n",
      "                              network=<interval> filesystem=<interval>\r\n",
      "  -c, --cpus-per-task=ncpus   number of cpus required per task\r\n",
      "      --checkpoint=time       job step checkpoint interval\r\n",
      "      --checkpoint-dir=dir    directory to store job step checkpoint image \r\n",
      "                              files\r\n",
      "      --comment=name          arbitrary comment\r\n",
      "  -d, --dependency=type:jobid defer job until condition on jobid is satisfied\r\n",
      "  -D, --chdir=path            change remote current working directory\r\n",
      "  -e, --error=err             location of stderr redirection\r\n",
      "      --epilog=program        run \"program\" after launching job step\r\n",
      "  -E, --preserve-env          env vars for node and task counts override\r\n",
      "                              command-line flags\r\n",
      "      --get-user-env          used by Moab.  See srun man page.\r\n",
      "      --gres=list             required generic resources\r\n",
      "  -H, --hold                  submit job in held state\r\n",
      "  -i, --input=in              location of stdin redirection\r\n",
      "  -I, --immediate[=secs]      exit if resources not available in \"secs\"\r\n",
      "      --jobid=id              run under already allocated job\r\n",
      "  -J, --job-name=jobname      name of job\r\n",
      "  -k, --no-kill               do not kill job on node failure\r\n",
      "  -K, --kill-on-bad-exit      kill the job if any task terminates with a\r\n",
      "                              non-zero exit code\r\n",
      "  -l, --label                 prepend task number to lines of stdout/err\r\n",
      "  -L, --licenses=names        required license, comma separated\r\n",
      "      --launch-cmd            print external launcher command line if not SLURM\r\n",
      "      --launcher-opts=        options for the external launcher command if not SLURM\r\n",
      "  -m, --distribution=type     distribution method for processes to nodes\r\n",
      "                              (type = block|cyclic|arbitrary)\r\n",
      "      --mail-type=type        notify on state change: BEGIN, END, FAIL or ALL\r\n",
      "      --mail-user=user        who to send email notification for job state\r\n",
      "                              changes\r\n",
      "      --mpi=type              type of MPI being used\r\n",
      "      --multi-prog            if set the program name specified is the\r\n",
      "                              configuration specification for multiple programs\r\n",
      "  -n, --ntasks=ntasks         number of tasks to run\r\n",
      "      --nice[=value]          decrease scheduling priority by value\r\n",
      "      --ntasks-per-node=n     number of tasks to invoke on each node\r\n",
      "  -N, --nodes=N               number of nodes on which to run (N = min[-max])\r\n",
      "  -o, --output=out            location of stdout redirection\r\n",
      "  -O, --overcommit            overcommit resources\r\n",
      "  -p, --partition=partition   partition requested\r\n",
      "      --prolog=program        run \"program\" before launching job step\r\n",
      "      --profile=value         enable acct_gather_profile for detailed data\r\n",
      "                              value is all or none or any combination of\r\n",
      "                              energy, lustre, network or task\r\n",
      "      --propagate[=rlimits]   propagate all [or specific list of] rlimits\r\n",
      "      --pty                   run task zero in pseudo terminal\r\n",
      "  -q, --quit-on-interrupt     quit on single Ctrl-C\r\n",
      "      --qos=qos               quality of service\r\n",
      "  -Q, --quiet                 quiet mode (suppress informational messages)\r\n",
      "  -r, --relative=n            run job step relative to node n of allocation\r\n",
      "      --restart-dir=dir       directory of checkpoint image files to restart\r\n",
      "                              from\r\n",
      "  -s, --share                 share nodes with other jobs\r\n",
      "      --slurmd-debug=level    slurmd debug level\r\n",
      "      --task-epilog=program   run \"program\" after launching task\r\n",
      "      --task-prolog=program   run \"program\" before launching task\r\n",
      "  -T, --threads=threads       set srun launch fanout\r\n",
      "  -t, --time=minutes          time limit\r\n",
      "      --time-min=minutes      minimum time limit (if distinct)\r\n",
      "  -u, --unbuffered            do not line-buffer stdout/err\r\n",
      "  -v, --verbose               verbose mode (multiple -v's increase verbosity)\r\n",
      "  -W, --wait=sec              seconds to wait after first task exits\r\n",
      "                              before killing job\r\n",
      "  -X, --disable-status        Disable Ctrl-C status feature\r\n",
      "      --switches=max-switches{@max-time-to-wait}\r\n",
      "                              Optimum switches and max time to wait for optimum\r\n",
      "\r\n",
      "Constraint options:\r\n",
      "      --contiguous            demand a contiguous range of nodes\r\n",
      "  -C, --constraint=list       specify a list of constraints\r\n",
      "      --mem=MB                minimum amount of real memory\r\n",
      "      --mincpus=n             minimum number of logical processors (threads) per node\r\n",
      "      --reservation=name      allocate resources from named reservation\r\n",
      "      --tmp=MB                minimum amount of temporary disk\r\n",
      "  -w, --nodelist=hosts...     request a specific list of hosts\r\n",
      "  -x, --exclude=hosts...      exclude a specific list of hosts\r\n",
      "  -Z, --no-allocate           don't allocate nodes (must supply -w)\r\n",
      "\r\n",
      "Consumable resources related options:\r\n",
      "      --exclusive             allocate nodes in exclusive mode when\r\n",
      "                              cpu consumable resource is enabled\r\n",
      "                              or don't share CPUs for job steps\r\n",
      "      --mem-per-cpu=MB        maximum amount of real memory per allocated\r\n",
      "                              cpu required by the job.\r\n",
      "                              --mem >= --mem-per-cpu if --mem is specified.\r\n",
      "      --resv-ports            reserve communication ports\r\n",
      "\r\n",
      "Affinity/Multi-core options: (when the task/affinity plugin is enabled)\r\n",
      "  -B, --extra-node-info=S[:C[:T]]           Expands to:\r\n",
      "      --sockets-per-node=S    number of sockets per node to allocate\r\n",
      "      --cores-per-socket=C    number of cores per socket to allocate\r\n",
      "      --threads-per-core=T    number of threads per core to allocate\r\n",
      "                              each field can be 'min' or wildcard '*'\r\n",
      "                              total cpus requested = (N x S x C x T)\r\n",
      "\r\n",
      "      --ntasks-per-core=n     number of tasks to invoke on each core\r\n",
      "      --ntasks-per-socket=n   number of tasks to invoke on each socket\r\n",
      "      --cpu_bind=             Bind tasks to CPUs\r\n",
      "                              (see \"--cpu_bind=help\" for options)\r\n",
      "      --hint=                 Bind tasks according to application hints\r\n",
      "                              (see \"--hint=help\" for options)\r\n",
      "      --mem_bind=             Bind memory to locality domains (ldom)\r\n",
      "                              (see \"--mem_bind=help\" for options)\r\n",
      "\r\n",
      "\r\n",
      "Help options:\r\n",
      "  -h, --help                  show this help message\r\n",
      "      --usage                 display brief usage message\r\n",
      "\r\n",
      "Other options:\r\n",
      "  -V, --version               output version information and exit\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "yhrun --help "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
